package com.window;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.generate.CodeFactory;

public class TableListGrid extends JFrame {
	// 得到显示器屏幕的宽高
	public int width = Toolkit.getDefaultToolkit().getScreenSize().width;
	public int height = Toolkit.getDefaultToolkit().getScreenSize().height;
	// 定义窗体的宽高
	public int windowsWidth = 800;
	public int windowsHeight = 500;

	private JTable table;

	JLabel fileLabel = new JLabel("生成目录");
	JTextField fileTxt = new JTextField("");// TextField 目录的路径
	JLabel packageLabel = new JLabel("包名称");
	JTextField packageTxt = new JTextField("");
	JButton fileBtn = new JButton("...");// 选择

	public TableListGrid() {

		fileTxt.setColumns(10);
		packageTxt.setColumns(10);

		fileBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jfc = new JFileChooser();// 文件选择器

				// 判断触发方法的按钮是哪个
				jfc.setFileSelectionMode(1);// 设定只能选择到文件夹
				int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
				if (state == 1) {
					return;
				} else {
					File f = jfc.getSelectedFile();// f为选择到的目录
					fileTxt.setText(f.getAbsolutePath());
					CodeFactory.generateFilePath = f.getAbsolutePath();
					System.out.println(f.getAbsolutePath());
				}

			}
		});

		setLayout(new BorderLayout());
		JPanel panelNorth = new JPanel();
		JButton createEntityBtn = new JButton("生成实体类");
		JButton createPageBtn = new JButton("生成页面");

		// jfc.setCurrentDirectory(new File("d://"));// 文件选择器的初始目录定为d盘

		createEntityBtn.setSize(60, 25);
		createPageBtn.setSize(60, 25);

		panelNorth.add(createEntityBtn);
		panelNorth.add(createPageBtn);
		panelNorth.add(fileLabel);
		panelNorth.add(fileTxt);
		panelNorth.add(fileBtn);
		panelNorth.add(packageLabel);
		panelNorth.add(packageTxt);

		add(panelNorth, BorderLayout.NORTH);
		// 设置窗口为最佳大小

		DefineTable defineTable = new DefineTable();

		setTitle("数据");
		this.setBounds((width - windowsWidth) / 2,
				(height - windowsHeight) / 2, windowsWidth, windowsHeight);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		table = new JTable(defineTable);
		JCheckBox jc1 = new JCheckBox();
		table.getColumnModel().getColumn(0)
				.setCellEditor(new DefaultCellEditor(jc1));

		table.setPreferredScrollableViewportSize(new Dimension(550, 30));

		// // 创建显示表格的滚动面板
		JScrollPane scrollPane = new JScrollPane(table);
		// // 将滚动面板添加到边界布局的中间
		getContentPane().add(scrollPane, BorderLayout.CENTER);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
//				System.exit(0);
				dispose();
			}
		});

		createEntityBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(fileTxt.getText() != null && fileTxt.getText().equals("")){
					JOptionPane.showMessageDialog(null, "请选择生成目录!", "提示", 2);
					return;
				}
				
				if(packageTxt.getText() != null && packageTxt.getText().equals("")){
					JOptionPane.showMessageDialog(null, "请填写package!", "提示", 2);
					return;
				}
				
				int rows = table.getRowCount();

				for (int i = 0; i < rows; i++) {
					if (table.getValueAt(i, 0).equals(true)) {
						System.out.println(table.getValueAt(i, 0));
						System.out.println(table.getValueAt(i, 1));
						System.out.println(table.getValueAt(i, 2));
						System.out.println(table.getValueAt(i, 3));
						System.out.println(table.getValueAt(i, 4));

						
						String tableCode = table.getValueAt(i, 1).toString();
						String tableZhName = table.getValueAt(i, 2).toString();
						String entityName = table.getValueAt(i, 3).toString();
						CodeFactory.genernateEntity(tableCode.toUpperCase(),tableZhName,entityName,packageTxt.getText());
						JOptionPane.showMessageDialog(null, "实体类生成成功！", "提示", 2);
					}
				}

				// System.out.println(table.getValueAt(row, 0));
				// System.out.println(table.getValueAt(row, 1));
			}
		});

		createPageBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});

	}

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		TableListGrid frame = new TableListGrid();

		// JPanel jpanel = new JPanel();
		// JButton confirmbutton = new JButton("生成");
		// jpanel.add(confirmbutton);
		// frame.add(jpanel);

		frame.setVisible(true);

	}

}